clear all
set_defaults _all
version 18
set more off


* add this to change directly to the location of the replication archive
cd 


********************************************************************************
* Install packages
********************************************************************************

* winsor
ssc install winsor

* rifreg 
ssc install rif

global p_winsior 0.025

cap program drop indicators
do StataSubCode/indicators.ado

********************************************************************************
* RPI
********************************************************************************

qui do StataSubCode/RPI.do

********************************************************************************
* Sweeps 0-3 (birth, 7, 11, 16, parental, teacher, medical, tests)
********************************************************************************

* controls collected in the PMS (at birth)
qui do StataSubCode/HDcontrols0.do 

* Controls collected from parents and a medical explamination at age 7 years
qui do StataSubCode/HDcontrols7.do

* household/health controls collected at 11
qui do StataSubCode/HDcontrols11.do 

* ability test scores at 7 and 11
qui do StataSubCode/testscores.do 

* school and pupil characterisitcs collected at 7 and 11 (controls) at 7
qui do StataSubCode/SchChar71116.do 

* teacher-rated skills at 7 
qui do StataSubCode/teacherratedskills.do 

* parental responses at 16
qui do StataSubCode/parentresp_16.do

* sweep 3 observation indicators
qui do StataSubCode/weights.do

* exams data
qui do StataSubCode/Exams.do

********************************************************************************
* Cohort member responses (age 16-55)
********************************************************************************

* Sweep 3 (age 16, 1973)
qui do StataSubCode/flat16.do

* Sweep 4 (age 23, 1981)
qui do StataSubCode/flat23.do 

* Sweep 5 (age 33, 1991)
qui do StataSubCode/flat33.do 

* Sweep 6 (age 42, 2000)
qui do StataSubCode/flat42.do 

* Sweep 7 (age 46, 2004)
qui do StataSubCode/flat46.do

* Sweep 8 (age 50, 2008)
qui do StataSubCode/unfolding50.do 
qui do StataSubCode/flat50.do 

* Sweep 9 (age 55, 2013)
qui do StataSubCode/unfolding55.do 
qui do StataSubCode/flat55.do 

* mapping for creative occupations
do StataSubCode/CreativeOccupations.do

********************************************************************************
* merge sweep-by-sweep data
********************************************************************************

use scratchdata/ncds0123_testscores.dta, clear

* teacher-rated skills
merge 1:1 NCDSID using scratchdata/ncds0123_teacherratedskills.dta, nogenerate

* hd controls collected at birth
merge 1:1 NCDSID using scratchdata/ncds0123_controlsbirth.dta, nogenerate

* hd controls collected at 7
merge 1:1 NCDSID using scratchdata/ncds0123_HDcontrols7.dta, nogenerate

* hd controls collected at 11
merge 1:1 NCDSID using scratchdata/ncds0123_HDcontrols11.dta, nogenerate

* school/teacher char at 7 and 11
merge 1:1 NCDSID using scratchdata/ncds0123_school711.dta, nogenerate

* age 55 outcomes
merge 1:1 NCDSID using scratchdata/ncds_2013_flatfile.dta, nogenerate

* 50 outcomes
merge 1:1 NCDSID using scratchdata/ncds_2008_flatfile.dta, nogenerate

* age 46 outcomes
merge 1:1 NCDSID using scratchdata/ncds_2004_flatfile.dta, nogenerate

* age 42 outcomes
merge 1:1 NCDSID using scratchdata/ncds_2000_flatfile.dta, nogenerate

* age 33 outcomes
merge 1:1 NCDSID using scratchdata/ncds_1991_flatfile.dta, nogenerate

* age 23 outcomes
merge 1:1 NCDSID using scratchdata/ncds_1981_flatfile.dta, nogenerate

* age 16 individual responses
merge 1:1 NCDSID using scratchdata/ncds_1974_flatfile.dta, nogenerate

* age 16 parental responses
merge 1:1 NCDSID using scratchdata/ncds_parental16.dta, nogenerate

* Sweep 3 observation indicators
merge 1:1 NCDSID using scratchdata/ncds_Sweep3Obs.dta, nogenerate

* Exams data
merge 1:1  NCDSID using scratchdata/ncds_1978_Exams1.dta



* code creative occupations
drop _merge
ge soc90 = soc90_I33
merge m:1 soc90 using scratchdata/CreativeOcc90.dta
drop if _merge==2
drop _merge
ge CreatOcc_I33 = pCreat90
drop pCreat90 soc90

ge soc90 = soc90_I42
merge m:1 soc90 using scratchdata/CreativeOcc90.dta
drop if _merge==2
drop _merge
ge CreatOcc_I42 = pCreat90
drop pCreat90 soc90

ge soc00 = soc00_I46
merge m:1 soc00 using scratchdata/CreativeOcc00.dta
drop if _merge==2
drop _merge
ge CreatOcc_I46 = pCreat00
drop pCreat00 soc00

ge soc00 = soc00_I50
merge m:1 soc00 using scratchdata/CreativeOcc00.dta
drop if _merge==2
drop _merge
ge CreatOcc_I50 = pCreat00
drop pCreat00 soc00

ge soc00 = soc00_I55
merge m:1 soc00 using scratchdata/CreativeOcc00.dta
drop if _merge==2
drop _merge
ge CreatOcc_I55 = pCreat00
drop pCreat00 soc00

********************************************************************************
* Analysis sample
********************************************************************************

* Non-missing test scores and teacher evaluations 
local tt creativityt_7 numeracyt_7 readingt_7 math_7 reading_7 reading_11 math_11 nonverbal_11 verbal_11 generalab_11
egen NM=rowmiss(`tt')

* Combine inforamtion on education and training
* do combineeduc.do

* total membership of the NCDS
count

count if InPMS==1

* NCDS members who were non part of the PMS
count if InPMS==0

*...and have full set of ability test scores
count if InPMS==1 & NM==0

*...and don't have full set of ability test scores
count if InPMS==1 & NM==1

* Indicator or analysis sample
keep if InPMS==1 & NM==0 

********************************************************************************
* Macro to define controls (first used in probabilityweights.do)
********************************************************************************

do StataSubCode/controls.do

********************************************************************************
* Clean test scores and ability ratings
********************************************************************************

* standardize test scores 
do StataSubCode/cleantestscores.do 

* clean teacher ratings and calculate teacher evaluation error
do StataSubCode/cleanteacherrating.do 

********************************************************************************
* Save combined data
********************************************************************************

save scratchdata/ncds_combined.dta, replace

********************************************************************************
* Descriptive statistics on test scores, creativity and controls
********************************************************************************

qui do StataSubCode/SummaryStats.do 

********************************************************************************
* Estimation
********************************************************************************

qui do StataSubCode/Estimation.do

********************************************************************************
* Clean up
********************************************************************************

erase scratchdata/RPI.dta
erase scratchdata/ncds0123_teacherratedskills.dta
erase scratchdata/ncds0123_testscores.dta
erase scratchdata/ncds0123_HDcontrols11.dta
erase scratchdata/ncds0123_school711.dta
erase scratchdata/ncds0123_controlsbirth.dta
erase scratchdata/ncds0123_HDcontrols7.dta
erase scratchdata/ncds_combined.dta
erase scratchdata/ncds_parental16.dta
erase scratchdata/ncds_Sweep3Obs.dta
erase scratchdata/ncds_2008_unfolding.dta
erase scratchdata/ncds_2013_flatfile.dta
erase scratchdata/ncds_2013_unfolding.dta
erase scratchdata/ncds_2004_flatfile.dta
erase scratchdata/ncds_2004_forwardfeed.dta
erase scratchdata/ncds_2008_flatfile.dta
erase scratchdata/ncds_1991_flatfile.dta
erase scratchdata/ncds_2000_flatfile.dta
erase scratchdata/ncds_1981_flatfile.dta
erase scratchdata/ncds_1974_flatfile.dta
erase scratchdata/ncds_1978_Exams1.dta
erase scratchdata/CreativeOcc90.dta
erase scratchdata/CreativeOcc00.dta


